#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//预定义数据结构
typedef struct stuInfo {
    char stuName[10]; /*学生姓名*/
    int Age; /*年龄*/
} ElemType;

typedef struct node {
    ElemType data;
    struct node *next;
} ListNode, *ListPtr;
ListPtr ListHead = NULL;//初始化表头

ListPtr CreateList(){
    ElemType *student = (ElemType*)malloc(sizeof(ElemType));
    strcpy(student->stuName,"NULL");
    student->Age = 0;
    ListPtr ln = (ListPtr)malloc(sizeof(ListNode));
    ln->data = *student;
    ln->next = NULL;
    printf("===========create a new list.===========\n");
    return ln;
}

void PrintList(ListPtr ListHead){
    if(ListHead==NULL){
        printf("===========there is no list.===========\n");
        return;
    }
    if(ListHead->next == NULL){
        printf("===========list is empty.===========\n");
        return;
    }
    ListPtr iter = ListHead->next;
    while(iter != NULL){
        ElemType stu = iter->data;
        printf("Name:%s, Age:%d\n", stu.stuName, stu.Age);
        iter = iter->next;
    }
}

void InsertList(ListPtr ListHead){
    if(ListHead==NULL){
        printf("===========there is no list.===========\n");
        return;
    }
    ListPtr iter = ListHead;
    while(iter->next != NULL){
        iter = iter->next;
    }
    ListPtr ln = (ListPtr)malloc(sizeof(ListNode));
    ElemType* student = &(ln->data);

    char stuName[10];
    int Age;
    printf("Please enter student name and age:\n");
    scanf("%s %d",stuName,&Age);
    getchar();//清除回车
    strcpy(student->stuName, stuName);
    student->Age = Age;

    ln->next = NULL;

    iter->next = ln;
}

int main(int argc,char argv[])
{
    while(1){
        printf("1 Create List\n");
        printf("2 Insert List\n");
        printf("3 Printf List\n");
        printf("4 Quit\n");
        char command = getchar();
        getchar();//清除回车
        switch (command)
        {
        case '1':
            ListHead = CreateList();
            break;
        case '2':
            InsertList(ListHead);
            break;
        case '3':
            PrintList(ListHead);
            break;
        case '4':{
            ListPtr iter = ListHead;
            ListPtr next;
            while(iter!=NULL){//释放内存空间
                next = iter->next;
                free(iter);
                iter = next;
            }
            return 0;
        }
        }
    }
}
